angular.module("app").component("winitParamsdiff", {
  templateUrl: getTemplateUrl("matrix/component/tpl/winitParamsdiff.html"),
  bindings: {
    paramTypes: "="
  },
  controller: function($scope) {
    var ctrl = this;
    ctrl.items = [
      { id: 1, title: "输入" },
      { id: 2, title: "输出" },
      { id: 3, title: "返回值对比" }
    ];
    ctrl.textjson = true;
    ctrl.toJSON = true;
    ctrl.toText = false;
    ctrl.switch = "toJSON";

    ctrl.paramTypeChange = function(item) {
      //入参
      ctrl.inParam = ctrl.selectParamType.param;
      //入参标记key
      ctrl.inChangeKeys = ctrl.selectParamType.paramKey;
      //出参
      ctrl.rtnParam = ctrl.selectParamType.rtn;
      //出参标记key
      ctrl.rtnChangeKeys = ctrl.selectParamType.rtnKey;

      ctrl.inText = $filter("json")(ctrl.inParam);
      ctrl.items[0].active = true;
      showInParams();
    };
    ctrl.addParam = function() {
      var modalInstance = $modal.open({
        animation: true,
        templateUrl: getTemplateUrl("matrix/servicegate/tpl/addsgmodal.html"),
        controller: "modalCtr",
        keyboard: true,
        backdrop: true,
        resolve: {
          data: function() {
            return (ctrl.selectParamType = {});
          }
        }
      });

      // 点击modal新增后，把feature值传过来
      modalInstance.result.then(
        function(data) {
          addData(data);
        },
        function(reason) {
          cancelModal(reason);
        }
      );
    };
    ctrl.updateParam = function() {
      ctrl.paramTypes
        .filter(a => a.crtname != ctrl.selectParamType.crtname)
        .push({
          crtname: ctrl.selectParamType.crtname,
          param: ctrl.inParam,
          rtn: ctrl.rtnParam,
          paramKey: ctrl.inChangeKeys,
          rtnKey: ctrl.rtnChangeKeys
        });
    };

    ctrl.switch2Text = function() {
      ctrl.toJSON = false;
      ctrl.toText = true;
      ctrl.switch = "toText";
      ctrl.inText = $filter("json")(ctrl.inParam);
      ctrl.rtnText = $filter("json")(ctrl.rtnParam);
    };
    $scope.switch2JSON = function() {
      ctrl.toJSON = true;
      ctrl.toText = false;
      ctrl.switch = "toJSON";
    };

    ctrl.changedInText = function() {
      ctrl.inParam = JSON.parse(ctrl.inText);
    };
    ctrl.changedRtnText = function() {
      ctrl.rtnParam = JSON.parse(ctrl.rtnText);
    };
    function cancelModal(reason) {
      console.log("取消");
    }
    function addData(data) {
      ctrl.selectParamType = {
        crtname: data,
        param: ctrl.inParam,
        rtn: ctrl.rtnParam,
        paramKey: ctrl.inChangeKeys,
        rtnKey: ctrl.rtnChangeKeys
      };
      ctrl.paramTypes.push({
        crtname: data,
        param: ctrl.inParam,
        rtn: ctrl.rtnParam,
        paramKey: ctrl.inChangeKeys,
        rtnKey: ctrl.rtnChangeKeys
      });
    }
  }
});

app.controller("modalCtr", [
  "$scope",
  "$rootScope",
  "$modalInstance",
  "$http",
  "$filter",
  "lodash",
  "data",
  function($scope, $rootScope, $modalInstance, $http, $filter, lodash, data) {
    $scope.modal = {};
    $scope.modal.inJson = data.param;
    $scope.modal.crtname = data.crtname;

    $scope.addParam = function() {
      $modalInstance.close($scope.modal.crtname);
    };

    $scope.cancel = function() {
      $modalInstance.dismiss("cancel");
    };
  }
]);
